home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / JAVA_UTL / HYPERPRO / SRC / PVS / UTILS / VLABEL.JAV < prev    next >
Encoding:
Text File  |  1996-09-14  |  2.6 KB  |  115 lines

  1. package PVS.Utils;
  2.  
  3. import java.awt.*;
  4.  
  5. public class VLabel extends Panel
  6. {
  7.   static public final int WEST_EAST = 0, SOUTH_NORTH = 1;
  8.  
  9.   String label;
  10.   int alignment;
  11.   int direction;
  12.   
  13.   public VLabel() {
  14.     this("");
  15.   }
  16.  
  17.   public VLabel(String label) {
  18.     this(label,Label.LEFT);
  19.   }
  20.  
  21.   public VLabel(String label, int alignment) {
  22.     this(label,alignment,WEST_EAST);
  23.   }
  24.  
  25.   public VLabel(String label, int alignment, int direction) {
  26.     this.label = label;
  27.     this.direction = direction;
  28.     this.alignment = alignment;
  29.   }
  30.     
  31.   public void setText(String label){
  32.     if (label != this.label && (this.label == null || !this.label.equals(label))) {
  33.       this.label = label;
  34.       memImage = null;
  35.       invalidate();
  36.       validate();
  37.     }
  38.   }
  39.   
  40.   public Dimension minimumSize() {
  41.     
  42.     //setFont(new Font(getFont().getName(),getFont().getStyle(),getFont().getSize()));
  43.     if (label != null) {
  44.       FontMetrics labelMetrics = getFontMetrics(getFont());      
  45.       
  46.       int labelWidth = labelMetrics.stringWidth(label);
  47.       int labelHeight = labelMetrics.getAscent()+labelMetrics.getDescent();
  48.             
  49.       if(direction == WEST_EAST)
  50.     return (new Dimension (labelWidth,labelHeight));
  51.       else if(direction == SOUTH_NORTH)
  52.     return (new Dimension (labelHeight,labelWidth));
  53.     } 
  54.     return new Dimension (0,0);
  55.   }
  56.  
  57.   public Dimension preferredSize() {
  58.     return minimumSize();
  59.   }
  60.   
  61.   public void update(Graphics g){
  62.     paint(g);
  63.   }
  64.  
  65.  
  66.   Image memImage; // image of this label in memory
  67.  
  68.   public void paint (Graphics g) {
  69.     if(memImage == null)
  70.       makeMemImage();
  71.     g.drawImage(memImage,0,0,null);
  72.   }       
  73.  
  74.   void makeMemImage(){
  75.     Dimension dim = size();
  76.     int width, height;
  77.     switch(direction){
  78.     default:
  79.     case WEST_EAST:
  80.       width = dim.width; height = dim.height;
  81.       break;
  82.     case SOUTH_NORTH:      
  83.       width = dim.height; height = dim.width;
  84.       break;
  85.     }
  86.  
  87.     memImage = this.createImage(width,height); 
  88.     Graphics memG = memImage.getGraphics();
  89.  
  90.     memG.setColor(getBackground());
  91.     memG.fillRect(0,0,width,height);
  92.  
  93.     memG.setFont(getFont());    
  94.     FontMetrics fm = getFontMetrics(getFont());
  95.     memG.setColor(getForeground());
  96.     memG.drawString (label, 0, fm.getAscent());
  97.     memG.dispose();
  98.  
  99.     if(direction == SOUTH_NORTH){
  100.       memImage = ImageUtils.rotateImage(memImage,90);
  101.     }    
  102.   }
  103.  
  104.   /*
  105.   public void paint(Graphics g) {
  106.     LabelPeer peer = (LabelPeer)this.getPeer();
  107.     //peer.paint
  108.     System.out.println("vlabel:paint");
  109.     g.setColor(Color.blue);
  110.     //g.fillRect(0,0,size().width,size().height);
  111.     //super.paint(g);
  112.   }
  113.   */
  114. }
  115.